package com.fusheng.demo

import android.annotation.SuppressLint
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import android.content.pm.PackageInfo

import android.content.pm.PackageManager
import java.util.*
import kotlin.collections.ArrayList
import android.net.wifi.WifiManager
import android.telephony.TelephonyManager
import android.view.View
import com.fusheng.demo.accessibilityservice.AutoShareWXService
import com.fusheng.utils.CheckRef
import com.fusheng.utils.DpUtil
import java.lang.Exception
import java.lang.reflect.Field
import java.lang.reflect.Method


class MainActivity : AppCompatActivity() {

    private var startTime :Long = 0

    @SuppressLint("MissingPermission")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        var ivIdentity = findViewById<ImageView>(R.id.ivIdentity)
        var tvInfo = findViewById<TextView>(R.id.tvInfo)


        tvInfo.text = "2022.09.10到期"
        tvInfo.setPadding(DpUtil.dp2px(63f), 0, 0, 0)
        tvInfo.setBackgroundResource(0)
//
        val layoutParams = ivIdentity.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.apply {
            width = DpUtil.dp2px(65f)
            height = DpUtil.dp2px(16f)
        }
        ivIdentity.setImageResource(R.mipmap.icon_mine_heiz_mermber_flag)

        val sb = StringBuilder()
        sb.append(
            "screenWidth:${DpUtil.getScreenWidth(this)}  , ${
                DpUtil.px2dp(
                    DpUtil.getScreenWidth(
                        this
                    ).toFloat()
                )
            }"
        )
        sb.append("\n")
        sb.append("screenHeight:${DpUtil.getScreenHeight(this)} ")
        sb.append("\n")
        sb.append("10dp:" + DpUtil.dp2px(10f))
        sb.append("\n")
        sb.append("1dp:" + DpUtil.dp2px(1f))
        sb.append("\n\n")

//        sb.append(getWlanMACaddress())

        sb.append("手机型号：${android.os.Build.MODEL} \n")
        sb.append("手机品牌：${android.os.Build.BRAND} \n")
        sb.append("手机厂商：${android.os.Build.MANUFACTURER} \n")
        sb.append("手机版本：${android.os.Build.VERSION.RELEASE} \n")
        sb.append("手机xx：${android.os.Build.FINGERPRINT} \n")

        sb.append("\n\n")

        sb.append(testForM())

//        reflect(Build())


        var phone =  getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        reflect(phone)

        Log.i("WWW", "screen info : \n$sb")

//        breakTest()

        findViewById<TextView>(R.id.showText).apply {
            text = sb.toString()
            setOnClickListener {
                startTime = System.currentTimeMillis()
                startActivity(Intent(this@MainActivity, IndexActivity::class.java))

                this@MainActivity.finish()
            }
        }
        findViewById<View>(R.id.btn_access).setOnClickListener {
            setOnClickListener()
            //无障碍模拟
//            AccessibilityUtils.instant.openService(
//                this@MainActivity,
//                AutoShareWXService::class.java
//            )


        }


        val ref =  CheckRef<Int>()
        ref.set(99)
        System.out.println("===============$ref")
    }


    //for循环验证代码
    fun breakTest() {
//        run outSide@{
        (0..10).forEachIndexed { index, i ->
            Log.d("WWW", "test start index=$index,i=$i")
            if (index >= 7) {
                return@forEachIndexed
            }
            Log.d("WWW", "test end index=$index,i=$i")
        }
//        }
        Log.d("WWW", "test end ===")
    }


    fun testForM(): String {

        var res = ""

        for (i in 1..10) {
            if (i == 4) {
                res += "--4--"
                break
            }
        }

        res += "--end--"

        return res
    }


    fun getWlanMACaddress(): String? {
        val wifiManager =
            this.getApplicationContext().getSystemService(WIFI_SERVICE) as WifiManager
        val wifiInfo = wifiManager.connectionInfo
        return wifiInfo?.macAddress
    }


    private fun getPkgListNew(): MutableList<String> {
        val packages: MutableList<String> = ArrayList()
        try {
            val packageInfos: List<PackageInfo> = this.getPackageManager().getInstalledPackages(
                PackageManager.GET_ACTIVITIES or
                        PackageManager.GET_SERVICES
            )
            for (info in packageInfos) {
                val pkg = info.packageName
                packages.add(pkg)
            }
        } catch (t: Throwable) {
            t.printStackTrace()
        }
        return packages
    }


    override fun onResume() {
        super.onResume()

        Log.e("MMMM", "==${this}== @onResume= ${System.currentTimeMillis()-startTime} ms")
    }

    override fun onPause() {
        super.onPause()

        Log.e("MMMM", "==${this}== @onPause= ${System.currentTimeMillis()-startTime} ms")
    }

    override fun onDestroy(){
        super.onDestroy()
        Log.e("MMMM", "==${this}== @onDestroy= ${System.currentTimeMillis()-startTime} ms")
    }


    fun setOnClickListener() {
        //获取剪切板管理器
        val cm: ClipboardManager =
            this.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        //设置内容到剪切板
        cm.setPrimaryClip(
            ClipData.newPlainText(
                null,
                "您的信任对云集（我们）非常重要，我们深知个人信息对您的重要性，我们非常重视用户的隐私和个人信息保护。您在下载"
            )
        )
    }


    @Throws(Exception::class)
    fun reflect(e: TelephonyManager) {
        val cls: Class<*> = e.javaClass
        val fields: Array<Field> = cls.declaredFields
        for (i in fields.indices) {
            val f: Field = fields[i]
            f.setAccessible(true)
            System.out.println("属性名:" + f.getName().toString() + " 属性值:" + f.get(e))
        }
    }


//    @Throws(Exception::class)
//    fun reflectMethod(e: TelephonyManager) {
//        val cls: Class<*> = e.javaClass
//        val fields: Array<Method> = cls.declaredMethods
//        for (i in fields.indices) {
//            val f: Method = fields[i]
//            f.setAccessible(true)
//            System.out.println("属性名:" + f.getName().toString() + " 属性值:" + f.invoke(e))
//        }
//    }
}